package com.kykj.kykj.controller;

import com.kykj.kykj.entity.ResultEntity;
import com.kykj.kykj.entity.RoleViewEntity;
import com.kykj.kykj.service.impl.AdminRoleServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("v2/adminrole/")
@CrossOrigin//解决跨域问题
@RestController
public class AdminRoleController {
    @Resource
    AdminRoleServiceImpl adminRoleService;

    /**
     * 获取角色列表(路径)
     * @param rolename 角色名称
     * @param routename 路径名称
     * @param limit 每页行数
     * @param page 页码
     * @return ResultEntity
     */
    @GetMapping("getrolelist")
    private ResultEntity getRoleList(
            @RequestParam("rolename") String rolename,
            @RequestParam("routename") String routename,
            @RequestParam("limit") int limit,
            @RequestParam("page") int page){
        ResultEntity resultEntity = new ResultEntity();
        try{
            int start =(page-1)*limit;//计算起始位置
            List<RoleViewEntity> list = adminRoleService.getRoleViewList(rolename, routename, start, limit);
            if(!rolename.equals("")||!routename.equals("")){ //有查询条件的情况下，查询所有符合条件的数量
                resultEntity.setCount(adminRoleService.getRoleViewListCountByName(rolename, routename));
            }else{//没有查询条件的情况下，查询所有的数据
                resultEntity.setCount(adminRoleService.getRoleViewCount());
            }
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
            resultEntity.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(9998);
            resultEntity.setMsg("数据获取失败！");
        }
        return resultEntity;
    }
}
